package com.yt.user.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.common.utils.Md5Utils;
import com.yt.user.common.config.YmlConfig;
import com.yt.user.mapper.StudentInfoMapper;
import com.yt.user.models.bo.StudentInfoBO;
import com.yt.user.models.bo.StudentInfoSaveBO;
import com.yt.user.models.dto.LoginInfoDTO;
import com.yt.user.models.dto.StudentInfoDTO;
import com.yt.user.models.service.ILoginInfoService;
import com.yt.user.models.service.IStudentInfoService;
import com.yt.user.models.vo.StudentInfoVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @dese
 * @date 2025/10/23 14:33
 */
@DubboService
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfoDTO> implements IStudentInfoService {

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private ILoginInfoService loginInfoService;
    @Resource
    private StudentInfoMapper studentInfoMapper;

    @Override
    public boolean add(StudentInfoSaveBO saveBO) {
        StudentInfoDTO dto = new StudentInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<StudentInfoVO> queryStudentList() {
        List<StudentInfoVO> voList = baseMapper.query();
        return voList;
    }

    @Override
    @Transactional
    public boolean addCus(StudentInfoSaveBO saveBO) {
        //1.增加客户信息
        StudentInfoDTO dto = new StudentInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        boolean flag = this.insert(dto);
        //2新增用户登录信息
        LoginInfoDTO loginInfoDTO = new LoginInfoDTO();
        //2.0客户id
        loginInfoDTO.setUserId(id);
        //2.1生成主键id
        loginInfoDTO.setId(ymlConfig.getPkId());
        //2.2账号
        loginInfoDTO.setAccount(saveBO.getPhone());
        //2.3密码盐
        String salt = Md5Utils.createSalt();
        loginInfoDTO.setSalt(salt);
        //2.4加密 密码
        //后台管理增加用户设置密码(默认密码为12345678z)
        if (saveBO.getPassword() == null) {
            loginInfoDTO.setPassword(Md5Utils.MD5Encode("12345678z", salt));
        } else {
            loginInfoDTO.setPassword(Md5Utils.MD5Encode(saveBO.getPassword(), salt));
        }
        loginInfoService.insert(loginInfoDTO);
        return flag;
    }

    @Override
    public Page<StudentInfoVO> queryStudentPage(Integer pageNo, Integer pageSize) {
        // 创建分页对象（指定页码和每页条数）
        Page<StudentInfoVO> page = new Page<>(pageNo, pageSize);
        // 调用Mapper查询列表（MyBatis-Plus会自动拦截SQL添加分页条件）
        List<StudentInfoVO> records = studentInfoMapper.queryPageVO(page);
        // 将查询结果设置到分页对象中
        page.setRecords(records);
        return page;
    }

    @Override
    public Page<StudentInfoDTO> queryStudentPage(StudentInfoBO bo) {
        Page<StudentInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        // 调用返回分页列表的mapper方法
        List<StudentInfoDTO> records = studentInfoMapper.queryPage(page, bo);
        // 将列表设置到Page对象（MyBatis-Plus会自动计算总条数）
        page.setRecords(records);
        return page;
    }
    @Override
    public List<StudentInfoDTO> selectUnallocatedStudents(){
        return studentInfoMapper.selectUnallocatedStudents();
    }

}
